<script>
import { useTodoStore } from '@/store/todoStore'
import { storeToRefs } from 'pinia'

export default {
  data() {
    const store = useTodoStore
    const { tasks } = storeToRefs(store)
    return {
      store,
      tasks,

      taskName: ''
    }
  },
  methods: {
    addTask() {
      if (this.taskName.length < 1) return
      this.store.addTask(this.taskName)
      this.taskName = ''
    },
    removeTask(taskId) {
      this.tasks = this.tasks.filter((task) => {
        return task.id !== taskId
      })
    }
  }
}
</script>
<template>
  <main>
    <h2>待办任务</h2>
    <input type="text" v-model="taskName" />
    <button @click="addTask" :disabled="taskName.length < 1">添加任务</button>
    <TransitionGroup name="list" tag="ul">
      <li v-for="task in tasks" :key="task.id">
        <div class="itemholder">
          <input type="checkbox" :key="task.id" v-model="task.finished" />
          <label :class="{ strikeout: task.finished }">{{ task.name }}</label>
          <Transition>
            <button
              class="del"
              @click="this.store.removeTask(task.id)"
              v-if="task.finished"
            >
              删除
            </button>
          </Transition>
        </div>
      </li>
    </TransitionGroup>
  </main>
</template>

<style>
main {
  margin: 20px 0;
  padding: 10px 0;
}
ul {
  padding: 0;
  list-style: none;
}
li {
  display: list-item;
  border: 1px solid #eee;
}
label {
  display: block;
  padding: 10px;
}
.strikeout {
  text-decoration: line-through;
}
input[type='text'] {
  margin: 5px 0;
  padding: 5px 10px;
  width: 70%;
}
button {
  margin: 5px 0;
  padding: 5px 10px;
  width: 30%;
}
input[type='checkbox'] {
  width: 20px;
  height: 20px;
}
.itemholder {
  display: flex;
  margin: 0 10px;
  align-items: center;
}
.del {
  margin-left: auto;
}
.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s ease;
}
.v-enter-from,
.v-leave-to {
  opacity: 0;
}
@keyframes jump-in {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}
.list-enter-active {
  animation: jump-in 0.5s;
}
.list-move,
.list-leave-active {
  transition: all 0.5s ease;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
.list-leave-active {
  position: absolute;
}
</style>
